package com.cwh.service.impl;

import java.util.List;

import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cwh.entity.Employee;
import com.cwh.entity.EmployeeCustom;
import com.cwh.entity.EmployeeExample;
import com.cwh.entity.EmployeeExample.Criteria;
import com.cwh.entity.SysRolePermission;
import com.cwh.entity.SysUserRole;
import com.cwh.mapper.EmployeeMapper;
import com.cwh.mapper.SysPermissionCustomMapper;
import com.cwh.mapper.SysPermissionMapperCustom;
import com.cwh.mapper.SysRolePermissionMapper;
import com.cwh.mapper.SysUserRoleMapper;
import com.cwh.service.EmployeeService;
import com.cwh.utils.MD5Utils;
import com.cwh.utils.UUIDUtils;

@Service(value="employeeService")
public class EmployeeServiceImpl implements EmployeeService {
	
	@Autowired
	private EmployeeMapper employeeMapper;
	
	@Autowired
	private SysPermissionMapperCustom sysPermissionMapperCustom;
	
	@Autowired
	private SysPermissionCustomMapper sysPermissionCustomMapper;
	
	@Autowired
	private SysUserRoleMapper sysUserRoleMapper;
	
	@Autowired
	private SysRolePermissionMapper sysRolePermissionMapper;
	
	//根据用户姓名查找用户
	@Override
	public Employee findEmployeeByName(String username) {
		//例子
		EmployeeExample example=new EmployeeExample();
		//创建规则对象
		Criteria createCriteria = example.createCriteria();
		createCriteria.andNameEqualTo(username);
		//根据输入的账号名称查询
		List<Employee> selectByExample = employeeMapper.selectByExample(example);
		Employee employee=null;
		if(selectByExample!=null&&selectByExample.size()>0) {
			
			employee = selectByExample.get(0);
		}
		return employee;
	}
	//查找所有用户
	@Override
	public List<Employee> findUserList() {
		return employeeMapper.selectByExample(null);
	}
	//用户列表自连接查上级名字联表查角色
	@Override
	public List<EmployeeCustom> findUserAndRoleList() {
		
		return sysPermissionMapperCustom.findUserAndRoleList();
	}

	//添加用户信息,给予默认角色与权力	
	@Override
	public void addEmployeeAssignRoleAndPermission(Employee employee) {
		//密码加密成密文
		String md5Password = new Md5Hash(employee.getPassword(),MD5Utils.MD5_SALT,MD5Utils.MD5_COUNT).toString();
		employee.setPassword(md5Password);
		//添加用户信息返回新插入的i
		employeeMapper.insertEmployeeReturnLastInsertId(employee);
		//为用户添加角色 将用户id与roleId保存到sys_user_role表
		SysUserRole record=new SysUserRole();
		record.setId(UUIDUtils.getRandomNumber());
		record.setSysRoleId(String.valueOf(employee.getRole()));
		record.setSysUserId(String.valueOf(employee.getId()));
		sysUserRoleMapper.insert(record);
		//为用户添加默认权限  将roleId和 permissionId（12,13,14）插入到sys_role_permission
		/*
		 * 12	报销申请
		 * 13	我的报销单
		 * 14	我的待办事务
		 * 不用设置了，角色对应的权限默认已经写在数据库中了
		 */


		
	}
	
	//通过id查找用户
	@Override
	public Employee findEmployeeById(Long id) {
		//例子
				EmployeeExample example=new EmployeeExample();
				//创建规则对象
				Criteria createCriteria = example.createCriteria();
				//根据managerId查询
				createCriteria.andIdEqualTo(id);
				List<Employee> selectByExample = employeeMapper.selectByExample(example);
				Employee employee=null;
				if(selectByExample!=null&&selectByExample.size()>0) {
					
					employee = selectByExample.get(0);
				}
				return employee;
	}
	

}
